package ynu.edu.Controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ynu.edu.Service.Impl.*;

import java.util.*;

@RestController
@RequestMapping("/sensor")
public class SensorController {
    @Resource
    WaterService waterService;
    @Resource
    TemperatureService temperatureService;
    @Resource
    NitrogenService nitrogenService;
    @Resource
    LightService lightService;
    @Resource
    CarbonService carbonService;
    @GetMapping("/SensorLastInfo")
    public String sensor(){
        List<Double> list = new ArrayList<>();
        list.add(waterService.getValue());
        list.add(temperatureService.getValue());
        list.add(nitrogenService.getValue());
        list.add(lightService.getValue());
        list.add(carbonService.getValue());
        return list.toString();
    }
    @GetMapping("/InfoList")
    public String infoList() throws JsonProcessingException {
        List<Double> carbonList = carbonService.getValuesFromLastDay();
        List<Double> tempList = temperatureService.getValuesFromLastDay();
        List<Date> timeList = carbonService.getTimeFromLastDay();

        Map<String, List<?>> resultMap = new HashMap<>();
        resultMap.put("co2", carbonList);
        resultMap.put("temp", tempList);
        resultMap.put("time", timeList);

        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(resultMap);
    }
}
